package com.auoem.radio.service;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.auctus.am.framework.constants.AmConstants;
import com.auctus.am.framework.entity.CallStatus;
import com.auctus.am.framework.gpio.RootCmdManager;
import com.auctus.am.framework.imp.RadioManager;
import com.auctus.am.framework.interfaces.IPttListener;
import com.auctus.am.framework.utils.LogM;
import com.auoem.radio.R;
import com.auoem.radio.base.AuConstants;
import com.auoem.radio.base.OemApplication;

import java.io.IOException;

/**
 * 对讲后台服务 xuyan
 * <p>
 * 20240409  后台服务。去除ptt页面的这个监听，只在服务里处理在main里启动，在app里注销。
 * 20240419  作为前台服务启动并发出通知
 */
public class PttService extends Service implements IPttListener {

    private static final String TAG = "PttService";
    private static final String CHANNEL_ID = "HighPriorityForegroundServiceChannel";

    @Override
    public void onCreate() {
        super.onCreate();

        registerListeners();

        createNotificationChannel();

        // 创建通知
        Notification notification = createNotification();

        // 启动服务为前台服务
        startForeground(1, notification);

        //TAG
        LogM.d(TAG, "PttService: onCreate");
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "HighPriorityForegroundService Channel",
                    NotificationManager.IMPORTANCE_HIGH
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }

    private Notification createNotification() {
        return new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("对讲前台服务")
                .setSmallIcon(R.drawable.ao_svg_nav_radio)
                .setPriority(NotificationCompat.PRIORITY_MAX)  // 设置最高优先级
                .build();
    }


    private void registerListeners() {
        if (!AmConstants.IS_DEBUG_BEFORE_DEV) {
            if (RadioManager.getInstance().getPttControl() != null) {
                RadioManager.getInstance().getPttControl().setPttListener(this);
            }
        }
    }

    @Override
    public void onDestroy() {
        unregisterListeners();
        super.onDestroy();
    }

    private final IBinder binder = new LocalBinder();

    public class LocalBinder extends Binder implements IBinder {
        PttService getService() {
            return PttService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }


    private void unregisterListeners() {
        if (!AmConstants.IS_DEBUG_BEFORE_DEV) {
            if (RadioManager.getInstance().getPttControl() != null) {
                RadioManager.getInstance().getPttControl().setPttListener(null);
            }
        }
    }

    // 实现IPttListener接口中的方法

    @Override
    public void onCallStatus(CallStatus callStatus) {
        LogM.d(TAG, "onCallStatus: " + callStatus.pttStatus);
        ctlGpioOnStatus(callStatus);
    }

    // 添加其他服务所需的方法
    private void ctlGpioOnStatus(CallStatus callStatus) {
        try {
            switch (callStatus.pttStatus) {
                case Wait:
                    try {
                        OemApplication.getApplication().getDevCtrlHorn1().PowerOffDevice();
                        OemApplication.getApplication().getDevCtrlHorn3().PowerOffDevice();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    //根据之前的状态控制GPIO，或者两者都关闭
                    RootCmdManager.getInstance().enablePath(AuConstants.AM_PATH_POWER_TX, false);
                    RootCmdManager.getInstance().enablePath(AuConstants.AM_PATH_POWER_MIC, false);
                    break;
                case Rev:
                    try {
                        OemApplication.getApplication().getDevCtrlHorn3().PowerOnDevice();
                        OemApplication.getApplication().getDevCtrlHorn1().PowerOnDevice();
                        OemApplication.getApplication().getDevCtrlHorn1().PowerOffDevice();
                        OemApplication.getApplication().getDevCtrlHorn1().PowerOnDevice();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    RootCmdManager.getInstance().enablePath(AuConstants.AM_PATH_POWER_TX, true);
                    break;
                case Snd:
                    try {
                        OemApplication.getApplication().getDevCtrlHorn1().PowerOffDevice();
                        OemApplication.getApplication().getDevCtrlHorn3().PowerOffDevice();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    RootCmdManager.getInstance().enablePath(AuConstants.AM_PATH_POWER_MIC, true);
                    break;
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception in ctlGpioOnStatus: " + e.getMessage());
            // 其他异常处理逻辑
        }
    }

}
